package com.bmt.pos.bean;

public class Role {
	/**
	 * 1 管理员 2 经理 4 店长 8 店员 16 主管
	 */
	public static final Role UNKNOW = new Role(0);
	public static final Role ADMIN = new Role(1);
	public static final Role MANAGER = new Role(2);
	public static final Role STORE_MANAGER = new Role(4);
	public static final Role CLERK = new Role(8);
	public static final Role SUPERVISOR = new Role(16);

	// public static final Role HR = new Role(32);
	// public static final Role NORMAL = new Role(64);
	// public static final Role HRMG = new Role(128);

	private Role(long roleCode) {
		this.code = roleCode;
	}

	/**
	 * 匹配角色包含role角色（子集）
	 * @param role
	 * @return
	 */
	public Boolean match(Role role) {
		return (code & role.code) == role.code;
	}

	/**
	 * 存在一种相同的角色（交集）
	 * @param role
	 * @return
	 */
	public Boolean like(Role role) {
		return (code & role.code) > 0;
	}
	
	/**
	 * 两个角色完全一样（等集）
	 * @param role
	 * @return
	 */
	public Boolean same(Role role) {
		return code == role.code;
	}

	private long code;

	public long getCode() {
		return code;
	}

	public static Role valueOf(long code) {
		if (code == UNKNOW.code)
			return UNKNOW;
		else if (code == ADMIN.code)
			return ADMIN;
		else if (code == MANAGER.code)
			return MANAGER;
		else if (code == STORE_MANAGER.code)
			return STORE_MANAGER;
		else if (code == CLERK.code)
			return CLERK;
		else if (code == SUPERVISOR.code)
			return SUPERVISOR;
		else
			return new Role(code);
	}

	public String getDisplayVal() {
		String str = "";
		if (match(ADMIN))
			str += "[Admin]";
		if (match(MANAGER))
			str += "[Manager]";
		if (match(STORE_MANAGER))
			str += "[Store_Manager]";
		if (match(CLERK))
			str += "[Clerk]";
		if (match(SUPERVISOR))
			str += "[Supervisor]";

		return str;
	}

	public Boolean isUnknow() {
		return match(UNKNOW);
	}

	public Boolean isAdmin() {
		return match(ADMIN);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof Role))
			return false;
		final Role other = (Role) obj;
		if (code != other.code)
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 1023;
		int result = 1;
		result = prime * result + (int) (code ^ (code >>> 32));
		return result;
	}
}
